package org.zsl.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONWriter;
import org.zsl.model.ResponseMsg;
import org.zsl.model.User;
import org.zsl.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class LoginController extends HttpServlet {

      private UserService userService = new UserService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User user = new User();

        String username = req.getParameter("username");
        String password = req.getParameter("password");

        System.out.println(username);
        System.out.println(password);
        user.setUsername(username);
        user.setPassword(password);

        /**
         *   登录名 = admin, 密码 = 123
         *   则返回 登录成功 - true -success
         * */


        resp.getWriter().write("success");

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User user = new User();


        String username = req.getParameter("username");
        String password = req.getParameter("password");

        user.setUsername(username);
        user.setPassword(password);
        /**
         * 返回文本的处理方式
         * */
       // Boolean isSuccess=   userService.login(user);
        // resp.getWriter().write(isSuccess.toString());

         /**
          *  返回一个对象的方式
          *     ResponseMsg
          * */

       ResponseMsg responseMsg =  userService.login2(user);

       resp.getWriter().write(JSON.toJSONString(responseMsg));

    }
}
